package com.jwthhealth_pub.old.physicalfitness.view;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.individual.module.ChangePhoneFirModule;
import com.jwthhealth_pub.old.individual.module.ChangePhoneSecModule;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.SendSms;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.jwthhealth_pub.common.MD5Util.getMD5;

/**
 * Created by Administrator on 2017/2/17.
 */

public class ChangePhoneNumActivity extends Activity {
    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.ed_sign_up_pw)
    EditText edSignUpPw;
    @BindView(R.id.ed_sign_up_confirm_pw)
    EditText edSignUpConfirmPw;
    @BindView(R.id.btn_sign_pw_commit)
    Button btnSignPwCommit;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.ed_sign_pw_verification_code)
    EditText edSignPwVerificationCode;
    @BindView(R.id.btn_get_verification_code)
    Button btnGetVerificationCode;
    @BindView(R.id.btn_sign_pw_confirm)
    Button btnSignPwConfirm;
    @BindView(R.id.ed_sign_pw_phone)
    EditText edSignPwPhone;
    private LoginModel.DataBean userinfoModel;
    private View setpOneLayout, setpTwoLayout;

    private int stepflag;
    private String changephonetoken;
    private String newPhone;
    private UserInfoDao userInfoDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone_num);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        userInfoDao = UserInfoDao.getInstance();
        userinfoModel = userInfoDao.queryUserinfoModel();

        setpOneLayout = findViewById(R.id.layout_pw_change);
        setpTwoLayout = findViewById(R.id.layout_pw_find);

        initClick();
        initView();
    }

    private void initView() {
//        edSignUpPw.setHint(R.string.individual_change_phone_num_hint);
//        edSignUpConfirmPw.setHint(R.string.individual_change_pw_hint);
//        edSignPwPhone.setHint(R.string.individual_new_phone_hint);
    }

    private void initClick() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                if (stepflag == 0) {
                    finish();
                } else {
                    chageStep(0);
                }
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    private void chageStep(int step) {
        if (step == 0) {
            stepflag = 0;
            setpOneLayout.setVisibility(View.VISIBLE);
            setpTwoLayout.setVisibility(View.GONE);
        } else {
            stepflag = 1;
            setpOneLayout.setVisibility(View.GONE);
            setpTwoLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.btn_sign_pw_commit, R.id.btn_get_verification_code, R.id.btn_sign_pw_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_sign_pw_commit:
                String phone = edSignUpPw.getText().toString().trim();
                if (phone.length() <= 0){
                    Toast.makeText(this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }

                String confirmPw = edSignUpConfirmPw.getText().toString().trim();
                if (confirmPw.length() <= 0) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }

//                if (!pw.equals(confirmPw)) {
//                    Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
//                }

                if (userinfoModel != null) {
                    Log.d("ChangePhoneNumActivity", confirmPw);
                    Log.d("ChangePhoneNumActivity", getMD5(confirmPw));
                    Call<ChangePhoneFirModule> changePhoneFirModuleCall = ApiHelper.changePhoneNumFirStep(userinfoModel.getId(), confirmPw, phone, userinfoModel.getAndroidtoken());
                    progressbar.setVisibility(View.VISIBLE);
                    changePhoneFirModuleCall.enqueue(new Callback<ChangePhoneFirModule>() {
                        @Override
                        public void onResponse(Call<ChangePhoneFirModule> call, Response<ChangePhoneFirModule> response) {
                            final ChangePhoneFirModule module = response.body();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (module.getCode().equals("0")){
                                        progressbar.setVisibility(View.GONE);
                                        changephonetoken = module.getData().getChangephonetoken();
                                        chageStep(1);
                                    } else {
                                        Toast.makeText(ChangePhoneNumActivity.this, module.getMsg(), Toast.LENGTH_SHORT).show();
                                        progressbar.setVisibility(View.GONE);
                                    }
                                }
                            });
                        }

                        @Override
                        public void onFailure(Call<ChangePhoneFirModule> call, Throwable t) {
                            Log.e("ChangePhoneNumActivity", "fail");
                        }
                    });
                } else {
                    Toast.makeText(this, "登陆状态异常", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btn_get_verification_code:
                newPhone = edSignPwPhone.getText().toString().trim();
                if (newPhone.length() <= 0) {
                    Toast.makeText(this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }

                Call<SendSms> setnewphone = ApiHelper.sendSms(newPhone, "setnewphone");
                setnewphone.enqueue(new Callback<SendSms>() {
                    @Override
                    public void onResponse(Call<SendSms> call, Response<SendSms> response) {
                        SendSms body = response.body();
                        if (body.getCode().equals("0")) {
                            Toast.makeText(ChangePhoneNumActivity.this, "请求已经发送", Toast.LENGTH_SHORT).show();
                        } else {
//                            Toast.makeText(ChangePhoneNumActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                            Log.d("ChangePhoneNumActivity", "fail");
                        }
                    }

                    @Override
                    public void onFailure(Call<SendSms> call, Throwable t) {
                        Toast.makeText(ChangePhoneNumActivity.this, "fail", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.btn_sign_pw_confirm:
                String code = edSignPwVerificationCode.getText().toString().trim();
                if (code.length() <= 0) {
                    Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }

                Call<ChangePhoneSecModule> changePhoneSecModuleCall = ApiHelper.changePhoneNumSecStep(userinfoModel.getId(), code, newPhone, changephonetoken, userinfoModel.getAndroidtoken());
                changePhoneSecModuleCall.enqueue(new Callback<ChangePhoneSecModule>() {
                    @Override
                    public void onResponse(Call<ChangePhoneSecModule> call, Response<ChangePhoneSecModule> response) {
                        ChangePhoneSecModule body = response.body();
                        if (body.getCode().equals("0")) {
                            Toast.makeText(ChangePhoneNumActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                            userInfoDao.updateUserPhone(newPhone);
                            finish();
                        } else {
                            Toast.makeText(ChangePhoneNumActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ChangePhoneSecModule> call, Throwable t) {
                        Log.e("ChangePhoneNumActivity", "fail");
                    }
                });

                break;
            default:break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            if (stepflag == 0) {
                finish();
            } else {
                chageStep(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
